/**
 * Created by Administrator on 2016/6/26.
 */
"use strict";
swan_pc
    .controller("CalShowController",function($scope,$rootScope,UserClip,TimeExchange,$timeout,$state,Clip,ClipList) {
        /*Clip.get({clipid:185},function(result){
            console.log(result);
        },function(result){
            console.log(result);
        })*/
        $scope.initCalendar=function(){
            var date = new Date();
            var d = date.getDate();
            var m = date.getMonth();
            var y = date.getFullYear();

            var calendar = $('#calendar').fullCalendar({
                header: {
                    left: 'prev,next today',
                    center: 'title',
                    right: 'month,agendaWeek,agendaDay'
                },
                //selectable: true,
                selectHelper: true,
                select: function(start, end, allDay) {
                    var title = prompt('Event Title:');
                    if (title) {
                        calendar.fullCalendar('renderEvent',
                            {
                                title: title,
                                start: start,
                                end: end,
                                allDay: allDay
                            },
                            true // make the event "stick"
                        );
                    }
                    calendar.fullCalendar('unselect');
                },
                //editable: true,
                //editable:true,
                droppable: true, // this allows things to be dropped onto the calendar !!!
                drop: function(date, allDay) { // this function is called when something is dropped

                    // retrieve the dropped element's stored Event Object
                    var originalEventObject = $(this).data('eventObject');

                    // we need to copy it, so that multiple events don't have a reference to the same object
                    var copiedEventObject = $.extend({}, originalEventObject);

                    // assign it the date that was reported
                    copiedEventObject.start = date;
                    copiedEventObject.allDay = allDay;
                    // is the "remove after drop" checkbox checked?
                    if ($('#drop-remove').is(':checked')) {
                        // if so, remove the element from the "Draggable Events" list
                        $(this).remove();
                    }
                },
                eventClick:function(event,jsEvent,view){
                    $rootScope.presentEventid=event.id;
                    if(event.bookerId!=null){
                        swal("好友为您book的日程您只能查看，不能编辑修改，您可以在左侧好友book日程列表中查看详情哦");
                    }else {
                        $state.go("clipdetail");
                    }
                },
                dayClick:function(date, allDay, jsEvent, view){
                    //date=date.toUTCString();
                    /*date=TimeExchange.UTCtoGMT(date);
                    console.log("GMT:"+date);
                    date=TimeExchange.GMTtoBjTime(date);
                    console.log("BjTime:"+date);*/
                    if($rootScope.toState.name=="companycalendar"){
                        $rootScope.clickedDate = date;
                        $state.go("companyaddcalendar");
                    }else if($rootScope.toState.name=="oppclip"){
                        $rootScope.clickedDate = date;
                        $state.go("oppaddclip");
                    }else {
                        $rootScope.clickedDate = date;
                        //$rootScope.clickDate=date;
                        $state.go("addclip");
                    }
                }
            })
        }
        $scope.initCalendar();
        $scope.clips = [];
        $scope.predicate = 'id';
        $scope.reverse = true;
        $scope.page = 1;
        $scope.loadAll = function () {
            var login = $rootScope.login;
            $scope.clips=[];
            $("#calendar").fullCalendar("removeEvents");
            Clip.query({page: $scope.page - 1, size: 1000},function(result, headers) {
                $scope.totalItems = headers('X-Total-Count');
                $scope.clips = result;
                angular.forEach($scope.clips, function (data) {
                    var startDate=new Date(data.startTime);
                    var endDate=new Date(data.endTime);
                    if (data.level != null && data.level != "") {
                        var clipLevel = data.level.toString();
                        var status=data.status.toString();
                        if (clipLevel == "主事项" && status=="CONFIRM") {
                            if($rootScope.toState.name=="companycalendar") {
                                console.log("id:" + companyId + " " + $scope.presentCompanyInfo.id);
                                if ($scope.presentCompanyInfo.id != null && $scope.presentCompanyInfo.id != "") {
                                    if (companyId == $scope.presentCompanyInfo.id) {
                                        var events = [
                                            {
                                                title: data.name,
                                                start: startDate,
                                                end: endDate,
                                                allDay: false,
                                                backgroundColor: Theme.colors.blue,
                                                id: data.id,
                                                bookerId:data.bookerId
                                            }
                                        ];
                                    }
                                }
                            }
                        }
                        if (clipLevel == "主事项" || clipLevel == "见缝插针") {
                                $("#calendar").fullCalendar("addEventSource", events);
                            }
                        }
                });
            });
        };
        $scope.loadAll();
        $scope.$parent.loadClipList();
    });